import { request } from './index'
import type { Project } from '@/types'

export const projectApi = {
  // 获取所有项目
  getAll(): Promise<Project[]> {
    return request.get('/projects')
  },

  // 根据ID获取项目
  getById(id: number): Promise<Project> {
    return request.get(`/projects/${id}`)
  },

  // 创建项目
  create(data: Omit<Project, 'id' | 'createdAt' | 'updatedAt'>): Promise<Project> {
    return request.post('/projects', data)
  },

  // 更新项目
  update(id: number, data: Partial<Project>): Promise<Project> {
    return request.put(`/projects/${id}`, data)
  },

  // 删除项目
  delete(id: number): Promise<void> {
    return request.delete(`/projects/${id}`)
  },

  // 获取我管理的项目
  getMyProjects(): Promise<Project[]> {
    return request.get('/projects/my')
  },

  // 根据状态获取项目
  getByStatus(status: string): Promise<Project[]> {
    return request.get(`/projects/status/${status}`)
  },

  // 更新项目状态
  updateStatus(id: number, status: string): Promise<Project> {
    return request.patch(`/projects/${id}/status`, { status })
  },

  // 搜索项目
  search(keyword: string): Promise<Project[]> {
    return request.get(`/projects/search?keyword=${keyword}`)
  },

  // 获取项目统计信息
  getStats(id: number): Promise<any> {
    return request.get(`/projects/${id}/stats`)
  },

  // 获取项目成员
  getMembers(id: number): Promise<any[]> {
    return request.get(`/projects/${id}/members`)
  },

  // 添加项目成员
  addMember(id: number, userId: number, role: string): Promise<void> {
    return request.post(`/projects/${id}/members`, { userId, role })
  },

  // 移除项目成员
  removeMember(id: number, userId: number): Promise<void> {
    return request.delete(`/projects/${id}/members/${userId}`)
  }
}